package com.entities;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.fetch.JSONParser;

public class Team {

	private static List<Team> teams;
	
	public Team(JSONObject json) {
		
		Long team_id;
		try {
			team_id = Long.valueOf(json.getString("name"));
		} catch (NumberFormatException e) {
			team_id = Long.valueOf(0);
		} catch (JSONException e) {
			team_id = Long.valueOf(0);
		}
		String name;
		try {
			name = json.getString("name");
		} catch (JSONException e) {
			name = "";
		}
		String tag;
		try {
			tag = json.getString("tag");
		} catch (JSONException e) {
			tag = "";
		}
		Long logo;
		try {
			logo = Long.valueOf(json.getString("logo"));
		} catch (NumberFormatException e) {
			logo = Long.valueOf(0);
		} catch (JSONException e) {
			logo = Long.valueOf(0);
		};
		Long player_0_account_id;
		try {
			player_0_account_id = Long.valueOf(json.getString("player_0_account_id"));
		} catch (NumberFormatException e) {
			player_0_account_id = Long.valueOf(0);
		} catch (JSONException e) {
			player_0_account_id = Long.valueOf(0);
		};;
		Long player_1_account_id;
		try {
			player_1_account_id = Long.valueOf(json.getString("player_1_account_id"));
		} catch (NumberFormatException e) {
			player_1_account_id = Long.valueOf(0);
		} catch (JSONException e) {
			player_1_account_id = Long.valueOf(0);
		};;
		Long player_2_account_id;
		try {
			player_2_account_id = Long.valueOf(json.getString("player_2_account_id"));
		} catch (NumberFormatException e) {
			player_2_account_id = Long.valueOf(0);
		} catch (JSONException e) {
			player_2_account_id = Long.valueOf(0);
		};;
		Long player_3_account_id;
		try {
			player_3_account_id = Long.valueOf(json.getString("player_3_account_id"));
		} catch (NumberFormatException e) {
			player_3_account_id = Long.valueOf(0);
		} catch (JSONException e) {
			player_3_account_id = Long.valueOf(0);
		};;
		Long player_4_account_id;
		try {
			player_4_account_id = Long.valueOf(json.getString("player_4_account_id"));
		} catch (NumberFormatException e) {
			player_4_account_id = Long.valueOf(0);
		} catch (JSONException e) {
			player_4_account_id = Long.valueOf(0);
		};;
		

		this.team_id = team_id;
		this.name = name;
		this.tag = tag;
		this.logo = logo;
		this.player_0_account_id = player_0_account_id;
		this.player_1_account_id = player_1_account_id;
		this.player_2_account_id = player_2_account_id;
		this.player_3_account_id = player_3_account_id;
		this.player_4_account_id = player_4_account_id;
	}
	public Long getTeam_id() {
		return team_id;
	}
	public String getName() {
		return name;
	}
	public String getTag() {
		return tag;
	}
	public Long getLogo() {
		return logo;
	}
	public Long getPlayer_0_account_id() {
		return player_0_account_id;
	}
	public Long getPlayer_1_account_id() {
		return player_1_account_id;
	}
	public Long getPlayer_2_account_id() {
		return player_2_account_id;
	}
	public Long getPlayer_3_account_id() {
		return player_3_account_id;
	}
	public Long getPlayer_4_account_id() {
		return player_4_account_id;
	}
	private Long team_id;
	private String name;
	private String tag;
	private Long logo;
	private Long player_0_account_id;
	private Long player_1_account_id;
	private Long player_2_account_id;
	private Long player_3_account_id;
	private Long player_4_account_id;
	
	public boolean checkPlayer(Long accId){
		if(player_0_account_id.equals(accId) || player_1_account_id.equals(accId) || player_2_account_id.equals(accId) || player_3_account_id.equals(accId) || player_4_account_id.equals(accId) ){
			return true;
		}
		else return false;
	}
	
	public static List<Team> getAllTeams(InputStream in){
		if(teams==null){ 
			teams = new ArrayList<Team>();
		try {
			JSONArray json = JSONParser.getJsonObjFromInputStream(in).getJSONArray("teams");
		for(int cont =0; cont < json.length(); cont++){
			JSONObject jo = json.getJSONObject(cont);
	
			teams.add(new Team(jo));
		}
		
		} catch (JSONException e) {
		}
		
		}
		return teams;
		
	}
	
}
